
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
using LPV_POS;
using LPV_POS.mh_pos;
public partial class mh_dm_HangHoa_chinhSua : Form
{
    string maHH = "";//PK_: giá trị này dc truyền từ bảng danh sách, trường hợp EDIT

    Boolean iloadform = false;

    #region"    mh_dm_HangHoa_chinhSua"
    public mh_dm_HangHoa_chinhSua(string _maHH)
    {
        InitializeComponent();
        maHH = _maHH;
    }
    #endregion

    #region"    frmDM_THE_Load"
    public void frmDM_THE_Load(object sender, EventArgs e)
    {
        string sSQL ="";
        try
        {
            thuvien othuvien = new thuvien();
            //////////////////////
            #region "   ShowComboBox"
            //com_nhomhang_chu
            sSQL =
                //"SELECT OBJID = NULL, OBJNAME = N' --- Chọn nhóm chủ hàng ---' "+
                //"UNION "+
                    "SELECT OBJID, OBJNAME FROM DM_NHOMCHUHH ";
            DataTable tbDM_NHOMCHU_HH = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            com_nhomhang_chu.Datasource = tbDM_NHOMCHU_HH;
            com_nhomhang_chu._Init();

            ////com_nhomhang
            //sSQL = 
            //      //  "SELECT OBJID = NULL, OBJNAME = N' --- Chọn nhóm hàng ---', MANHOMCHU = NULL, VAT = NULL " +
            //      //"UNION " +
            //      "SELECT OBJID, OBJNAME, MANHOMCHU, VAT FROM DM_NHOMHH";

            //DataTable tbDM_NHOMHH = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL"}, new object[] { sSQL});
            //com_nhomhang.Datasource = tbDM_NHOMHH;
            //com_nhomhang._Init();

            //COM_VAT
            sSQL = "SELECT OBJID = NULL, OBJNAME = ' VAT' " +
                    "UNION " +
                    "SELECT OBJID, OBJNAME FROM DM_VAT ";
            DataTable tbDM_VAT = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            com_vathanghoa.Datasource = tbDM_VAT;
            com_vathanghoa._Init();

            //COM_MAYIN_CHEBIEN
            sSQL = "SELECT * FROM DM_MAYIN_BEP ORDER BY COMNAME";
            DataTable tbDM_MAYIN_BEP = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            othuvien.f_showdataoncombox(com_MayIn_CheBien, tbDM_MAYIN_BEP, "DM_MAYIN_BEP_MA", "OBJNAME");

            //DM_DVT
            sSQL = "SELECT OBJID, OBJNAME FROM DM_DVT";
            DataTable tbDM_DVT = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            com_DVT.Datasource = tbDM_DVT;
            com_DVT._Init();
            #endregion

            #region "   ShowInfo"
            sSQL = "SELECT a.OBJID, a.SHORTID, a.OBJNAME, a.MANHOMHH, a.VAT, a.MADVT, a.GIA1, b.MANHOMCHU, "+
                    "   DM_MAYIN_BEP_MA = ISNULL(a.DM_MAYIN_BEP_MA,  c.DM_MAYIN_BEP_MA) " +
                    "FROM DM_HH a " +
                    "   LEFT JOIN DM_NHOMHH b ON b.OBJID = a.MANHOMHH " +
                    "   LEFT JOIN DM_NHOMCHUHH c ON b.MANHOMCHU = c.OBJID " +
                    "WHERE a.OBJID LIKE '" + maHH + "'";
            DataTable tbDM_HH = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            if (tbDM_HH.Rows.Count > 0)
            {
                float igia1 = 0;
                float.TryParse(tbDM_HH.Rows[0]["GIA1"] + "", out igia1);
                txt_gia1.Text = igia1.ToString("N0");

                txt_tenhanghoa.Text = tbDM_HH.Rows[0]["OBJNAME"] + "";
                txtMaCode.Text = tbDM_HH.Rows[0]["SHORTID"] + "";
                //
                com_DVT._SetselectedID(tbDM_HH.Rows[0]["MADVT"] + "");
                com_MayIn_CheBien._SetselectedID(tbDM_HH.Rows[0]["DM_MAYIN_BEP_MA"] + "");
                com_nhomhang_chu._SetselectedID(tbDM_HH.Rows[0]["MANHOMCHU"] + "");
                com_nhomhang._SetselectedID(tbDM_HH.Rows[0]["MANHOMHH"] + "");
                com_vathanghoa._SetselectedID(tbDM_HH.Rows[0]["VAT"] + "");
            }
            #endregion
        }
        catch (Exception ex)
        {
            new mh_thongbao("frmDM_THE_Load:\n" + ex.Message, 2).ShowDialog();
        }
        iloadform = true;
    }
    #endregion

    private void btnluu_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;

        int iCode = 0;
        int.TryParse(txtMaCode.Text, out iCode);
        float igia = 0;
        float.TryParse(txt_gia1.Text, out igia);
        if (igia > 2170000000)
        {
            LPV_POS.Module.MessageBox_LPV.Show("Giá phải nhỏ hơn 2,170,000,000.");
            return;
        }
        thuvien othuvien = new thuvien();
        othuvien.SP_Update("sp_DM_HH",
                            new string[] { "@SHORTID", "@OBJNAME", "@MANHOMHH", 
                                            "@VAT", "@MADVT", "@GIA1" ,
                                            "@OBJID","@flag", "@DM_MAYIN_BEP_MA"},
                            new object[] { iCode, txt_tenhanghoa.Text, com_nhomhang._GetselectedID(),
                                            com_vathanghoa._GetselectedID(), com_DVT._GetselectedID(), igia,
                                            maHH,1,com_MayIn_CheBien._GetselectedID()});

        this.Close();
    }

    private void btnDSthoat_Click(object sender, EventArgs e)
    {
        this.Dispose();
    }

    private void bton_gia1_Click(object sender, EventArgs e)
    {
        txt_gia1.Text = new cpublic().loadformso_point(300,300);
        txt_gia1.Focus();
    }

    private void bton_macode_Click(object sender, EventArgs e)
    {
        txtMaCode.Text = new cpublic().loadformso_point(200, 200);
        txtMaCode.Focus();
    }

    private void bton_tenhanghoa_Click(object sender, EventArgs e)
    {
        txt_tenhanghoa.Text = new cpublic().loadformchu_point(300, 200);
    }

    private void com_nhomhang_chu_SelectedIndexChanged(object sender, EventArgs e)
    {
        //com_nhomhang
       string sSQL =
            //  "SELECT OBJID = NULL, OBJNAME = N' --- Chọn nhóm hàng ---', MANHOMCHU = NULL, VAT = NULL " +
            //"UNION " +
              "SELECT OBJID, OBJNAME, MANHOMCHU, VAT FROM DM_NHOMHH WHERE MANHOMCHU LIKE '" + com_nhomhang_chu._GetselectedID() + "' ORDER BY OBJNAME ";
       thuvien othuvien = new thuvien();
        DataTable tbDM_NHOMHH = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
        com_nhomhang.Properties.Items.Clear();
        othuvien.f_showdataoncombox(com_nhomhang, tbDM_NHOMHH, "OBJID", "OBJNAME");
        //com_nhomhang.Datasource = tbDM_NHOMHH;
        //com_nhomhang._Init();
    }

  

}

